package org.jxf.gps.jfinal.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.jfk.core.utils.map.google.GpsCorrect;
import org.jxf.gps.jt808.server.GpsServer;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class GpsController extends Controller{
	public void index(){
	}
	
	public void loadVehicle(){
		String vehicle_mdn = this.getPara("vehicle_mdn");
		Record record = Db.findFirst("select ifnull(a.plateNum,'-') plateNum,"
				+ "ifnull(a.vehicleName,'-') vehicleName,"
				+ "ifnull(a.driver,'-') driver,"
				+ "ifnull(b.mdn,'-') mdn,"
				+ "ifnull(b.phone_num,'-') phone_num,"
				+ "b.lat,b.lng,"
				+ "b.gpsTime,b.statInfo,b.alertInfo,b.onlineTime "
				+ "from vehicle a,gpsTerminal b where a.terminalId=b.id and b.mdn = ?",vehicle_mdn);
		if (record == null){
			record = new Record();
			record.set("errorMsg", "no data");
			this.renderJson(record);
			return;
		}
		double wsLat = record.getDouble("lat");
		double wsLng = record.getDouble("lng");
		double[] lngLat = new double[2];
		GpsCorrect.transform(wsLat, wsLng, lngLat);
		record.set("lat", lngLat[0]);
		record.set("lng", lngLat[1]);
		
		List<Record> list = Db.find("select a.id,a.mdn,a.lat,a.lng,a.speed,a.alt,a.direct, "
				+ " a.gpsTime,a.statInfo,a.alertInfo,a.alertDesc ,b.phone_num,c.plateNum "
				+ " from alertinfo a ,gpsterminal b,vehicle c "
				+ " where a.mdn = b.mdn and b.id = c.terminalId and a.alertDesc is not null and a.mdn = ? order by gpstime desc LIMIT 10",vehicle_mdn);
		record.set("alertList", list);
		this.renderJson(record);
	}

	/**
	 * 获取车辆列表及动态数据，由index.jsp通过ajax调用,并定时调用
	 */
	public void loadVehicleList(){
		String groupIds = null;
		String groupPath = null;
		Map m = new HashMap();
		

		

		List list = GpsServer.orgSessionMap.get(groupPath);
		if (list == null){
			list = new ArrayList<HttpSession>();
			list.add(this.getSession());
		}else{
			if (!list.contains(this.getSession())){
				list.add(this.getSession());
			}
		}
		GpsServer.orgSessionMap.put(groupPath, list);
		
		String sql = "select a.id vid,b.id tid,c.id  gid,"
				+ "a.plateNum,"
				+ "ifnull(a.vehicleName,'-') vehicleName,"
				+ "b.mdn,"
				+ "b.phone_num,"
				+ "a.driver "
				+ "from vehicle a,gpsTerminal b,hi_org c "
				+ "where a.terminalId=b.id and a.groupId=c.id and c.id in ("+groupIds+")";
		
		String key = this.getPara("key");

		if (key != null && key.length() > 0){
			sql = sql + " and ( a.plateNum like '%" + key + "%' or b.mdn like '%"+ key +"%')";
		}
		
		List<Record> vehicleList = Db.find(sql);
		
		List<Record> groupList = Db.find("select id,orgName,parentOrg from hi_org c where c.id in ("+groupIds+")");
		
		int onlineCount = 0;
		for (int i = 0 ; i < vehicleList.size() ; i++){
			Record rc = vehicleList.get(i);
			String mdn = rc.getStr("mdn");
			if (GpsServer.onlineTerminals.get(mdn) != null){
				onlineCount++;
				rc.set("online", "online");
			}else{
				rc.set("online", "offline");
			}
			vehicleList.set(i, rc);
		}
		
		
		m.put("vehicleList", vehicleList);
		m.put("groupList", groupList);
		m.put("onlineCount", onlineCount);

		/**
		 * 获取告警信息列表
		 */
		List<Map> listMsg = GpsServer.sessionMessage.get(this.getSession());
		if (listMsg != null && listMsg.size() > 0){
			m.put("alertList", listMsg);
		}
		this.renderJson(m);

		GpsServer.sessionMessage.remove(this.getSession());
	}
	
	public void loadVehiclesOnGMap(){
		String vehicleMdns = this.getPara("vehicleMdns");
		if (vehicleMdns == null || vehicleMdns.trim().length()==0){
			vehicleMdns = "''";
		}
		//List<Record> list = Db.find("select a.plateNum,b.mdn,b.lat,b.lng from vehicle a,gpsTerminal b where a.terminalId=b.id and b.mdn in (?)",vehicleMdns);
		List<Record> list = Db.find("select a.plateNum,b.mdn,b.lat,b.lng from vehicle a,gpsTerminal b where a.terminalId=b.id and b.mdn in ("+vehicleMdns+")");

		for (int i =0 ; i < list.size() ; i++){
			double wsLat = list.get(i).getDouble("lat").doubleValue();
			double wsLng = list.get(i).getDouble("lng").doubleValue();
			double[] lngLat = new double[2];
			GpsCorrect.transform(wsLat, wsLng, lngLat);
			list.get(i).set("lat", lngLat[0]);
			list.get(i).set("lng", lngLat[1]);
		}
		this.renderJson(list);
		
	}
	
	public void loadTraceById(){
		String tradeId = this.getPara("traceId");
		Record record = Db.findById("trace", tradeId);
		if (record == null){
			record = new Record();
			record.set("errorMsg", "no data");
			this.renderJson(record);
			return;
		}		
		this.renderJson(record);
	}
	
	/**
	 * 轨迹查询
	 * 
	 */
	public void loadTrace(){
		String vehicle_mdn = this.getPara("vehicle_mdn");
		String startTime = this.getPara("startTime");
		String endTime = this.getPara("endTime");
		List<Record> list = Db.find("select * from trace where mdn = ? and gpsTime between ? and ?",vehicle_mdn,startTime,endTime);
		//@todo 超过1万多条记录时会内存溢出。
		for (int i =0 ; i < list.size() ; i++){
			double wsLat = list.get(i).getDouble("lat").doubleValue();
			double wsLng = list.get(i).getDouble("lng").doubleValue();
			double[] lngLat = new double[2];
			GpsCorrect.transform(wsLat, wsLng, lngLat);
			list.get(i).set("lat", lngLat[0]);
			list.get(i).set("lng", lngLat[1]);
		}
		this.setAttr("list", list);
		this.renderJson(list);
	}

}
